import time
import unittest
# import win32api
# import win32con
from selenium.webdriver.support.ui import Select  # 导入select下拉框
import os.path
import xlrd
import yaml
import openpyxl
import sys
# 鼠标事件包
import requests
from urllib import request
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class gtly:

    def getDriver(self,path,row_star,row_end,indexName):

        # 坐标从0开始的，row_star,row_end需要-1，不过我程序自动减少了
        row_star = row_star-1
        row_end = row_end -1

        options = webdriver.ChromeOptions()
        options.add_experimental_option('detach', True)  # 不自动关闭浏览器
        # options.add_argument('--start-maximized')  # 浏览器窗口最大化

        self.driver = webdriver.Chrome(options=options)

        self.driver.get(r'https://cbs.guojutech.net')
        self.driver.implicitly_wait(10)

        # 点击验证码登录
        self.driver.find_element(By.XPATH, '//*[@id="app"]/div/section/div[2]/div[2]/div/div[3]/form/div[6]/p').click()

        # 输入许婷的手机号
        self.driver.find_elements(By.CLASS_NAME, 'el-input__inner')[0].send_keys(16687771200)

        # 输入验证码随便你输入
        self.driver.find_elements(By.CLASS_NAME, 'el-input__inner')[1].send_keys(1234)

        # 点击登录
        self.driver.find_element(By.CLASS_NAME, 'btnBox').click()


        # 登录之后多等几秒
        # time.sleep(5)
        # token = self.driver.execute_script('return localStorage.getItem("x-auth-token")')
        # return token

        # 判断是不是缘多不是的话选缘多企业,先要鼠标悬停
        # 判断当前企业是不是缘多
        print('当前企业是'+self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div/div[1]/div[2]/div[1]/div[2]/div[2]/div[2]/span').text)#看看当前企业是什么
        if(self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div/div[1]/div[2]/div[1]/div[2]/div[2]/div[2]/span').text != '小马国炬（测试）'):
            #鼠标悬停
            xt = self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div/div[1]/div[2]/div[1]/div[2]/div[2]/div[2]')

            ActionChains(self.driver).move_to_element(xt).perform()

            list = self.driver.find_elements(By.XPATH, '//*[@class="el-dropdown-menu el-popper enterpriseList"]/li')
            for i in list:
                if i.text == '小马国炬（测试）':
                    i.click()
                    print('小马国炬（测试）')
                    break
        #找到公铁联运菜单
        for menu in self.driver.find_elements(By.XPATH,'//*[@id="app"]/div/div[2]/div[1]/div/ul/li/p/span'):
            if menu.text == '公铁联运':
                menu.click()
                break
        time.sleep(1)
        for menu_two in self.driver.find_elements(By.XPATH,'//*[@id="app"]/div/div[2]/div[1]/div/ul/li[17]/ul/ul/div/div/div/li/span'):
            if menu_two.text == '发运台账':
                menu_two.click()
                break

        #进入了嵌入式iframe，需要先进入
        #driver.switch_to_default.content()  # 从frame中返回到原来的地方
        # iframes = driver.find_elements(By.CSS_SELECTOR,'iframe')
        # print(len(iframes))
        # time.sleep(5)
        self.driver.switch_to.frame("iframe1")  # 进入到小窗口,嵌入式html必须 要去找frame
        # #点击新增台账
        self.driver.find_element(By.XPATH,'/html/body/div/section/section/section/div/div[1]/div/div[2]/div[1]/div[1]/div/div[2]/div[1]/button[1]').click()




        #开始读取excel文件
        data = xlrd.open_workbook(path)
        table = data.sheet_by_name(indexName)


        i = 1
        y = 1
        row_one = row_star
        while row_star <= row_end:
            #是否要点击添加按钮
            if row_one != row_star:
                self.driver.find_element(By.CLASS_NAME, 'el-icon-plus').click()

            # 写入客户
            excel_user = str(table.cell(row_star, 1).value)
            # 点击下拉框
            self.driver.find_element(By.XPATH,f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[2]/div/div/div/div/div/div[1]/input').send_keys(excel_user)
            y = y + 1
            #time.sleep(1)                                   #/html/body/div[9]/div[1]/div[1]/ul/li[2]/span  [not(@style="display: block;")]style="display: none;"
            time.sleep(0.5)
            #print(len(self.driver.find_elements(By.XPATH, f"/html/body/div[{y}]/div[1]/div[1]/ul/li[not(@style='display: none;')]/span")))
            for key in self.driver.find_elements(By.XPATH, f"/html/body/div[{y}]/div[1]/div[1]/ul/li[not(@style='display: none;')]/span"):
                if excel_user == key.text:
                    key.click()
                    break
            if excel_user != key.text:
                return print('客户信息在系统中没有，请先添加')



            #写入收货单位
            excel_unit = str(table.cell(row_star, 3).value)
            # 点击下拉框
            self.driver.find_element(By.XPATH,f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[3]/div/div/div/div/div/div[1]/input').send_keys(
                excel_unit)
            time.sleep(1)                                   #/html/body/div[9]/div[1]/div[1]/ul/li[1]/span
            y = y + 1
            for key in self.driver.find_elements(By.XPATH, f'/html/body/div[{y}]/div[1]/div[1]/ul/li/span'):
                if excel_unit == key.text:
                    key.click()
                    break
            if excel_unit != key.text:
                return print('收货单位在系统中没有，请先添加')

            #写入货物名称
            excel_name = str(table.cell(row_star, 2).value)
            # 点击下拉框                          //*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr/td[4]/div/div/div/div/div/div/div/input
            self.driver.find_element(By.XPATH,f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[4]/div/div/div/div/div/div/div/input').send_keys(excel_name)
            time.sleep(1)
            y = y + 1
            for key in self.driver.find_elements(By.XPATH, f'/html/body/div[{y}]/div[1]/div[1]/ul/li/span'):
                if excel_name == key.text:
                    key.click()
                    break
            if excel_name != key.text:
                return print('货物名称在系统中没有，请先添加')

            #写入承运物流
            excel_wuliu = str(table.cell(row_star, 4).value)
            # 输入                               //*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr/td[5]/div/div/div/div/div[1]/input
            self.driver.find_element(By.XPATH,f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[5]/div/div/div/div/div[1]/input').clear()
            self.driver.find_element(By.XPATH,f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[5]/div/div/div/div/div[1]/input').send_keys(
                excel_wuliu)

            #写入车牌号
            excel_carNo = str(table.cell(row_star, 7).value)
            # 输入                               //*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr/td[5]/div/div/div/div/div[1]/input
            self.driver.find_element(By.XPATH,f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[6]/div/div/div/div/div/input').send_keys(
                excel_carNo)

            # 写入箱号
            excel_num = str(table.cell(row_star, 8).value).split('.')[0]
            print(excel_num)
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[7]/div/div/div/div/div/input').click()
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[7]/div/div/div/div/div/input').send_keys(
                excel_num)
            # 写入承运吨位
            excel_weight = str(table.cell(row_star, 9).value)
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[8]/div/div/div/div/div/input').click()
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[8]/div/div/div/div/div/input').send_keys(
                excel_weight)
            #写入装箱时间
            excel_time = str(table.cell(row_star, 6).value)
            time_item = time.strptime(excel_time, "%Y.%m.%d")
            excel_newtime = str(time_item.tm_year) + '-' + str(time_item.tm_mon) + '-' + str(time_item.tm_mday)
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[9]/div/div/div/div/div/input').click()
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[9]/div/div/div/div/div/input').send_keys(
                excel_newtime)
            y = y + 1




            #起运地
            excel_address_start = str(table.cell(row_star, 11).value)
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[11]/div/div/div/div/div/input').clear()
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[11]/div/div/div/div/div/input').click()
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[11]/div/div/div/div/div/input').send_keys(
                excel_address_start)

            #到达地
            excel_address_end = str(table.cell(row_star, 12).value)
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[12]/div/div/div/div/div/input').clear()
            self.driver.find_element(By.XPATH,
                                     f'//*[@id="app"]/section/section/section/div/div/div/form/div[2]/div/div[3]/table/tbody/tr[{i}]/td[12]/div/div/div/div/div/input').send_keys(
                excel_address_end)

            row_star = row_star + 1
            i = i + 1




if __name__ == '__main__':
    gtly = gtly()
    #gtly.red('D:/tools/pythonItem/python/python_ty/gtly/data/缘多发运台账.xlsx','1月')
    #print(gtly.getDriver('D:/tools/pythonItem/python/python_ty/gtly/data/缘多发运台账.xlsx',1445,1445,'2023.2月 '))
    gtly.getDriver('D:/tools/pythonItem/python/python_ty/gtly/data/缘多发运台账.xlsx',1698,1719,'2023.2月 ')


